package com.example.initializesql;


import android.Manifest;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.http.HTTP;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rlv)
    RecyclerView mRlv;
    private ArrayList<Bean> beans;
    private RlvAdapter rlvAdapter;
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initListener();
        initPermiss();
    }

    private void initListener() {
        rlvAdapter.setOnItemLongClickListener(new RlvAdapter.onItemLongClickListener() {
            @Override
            public void onItemLongClick(int position) {
                mPosition = position;
            }
        });
    }

    private void initView() {
        mRlv.setLayoutManager(new LinearLayoutManager(this));
        mRlv.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        beans = new ArrayList<>();
        setData();
        rlvAdapter = new RlvAdapter(this, beans);
        mRlv.setAdapter(rlvAdapter);
        registerForContextMenu(mRlv);
    }

    private void setData() {
        MySql mySql = new MySql(this, "my.db", null, 1);
        for (int i = 0; i < 20; i++) {
            Bean bean1 = new Bean(i, "张三" + i, "男", 12 + i);
            beans.add(bean1);
            mySql.insert(bean1);
        }

        Toast.makeText(this, "插入数据成功", Toast.LENGTH_SHORT).show();
        mySql.close();
    }

    private void initPermiss() {
        PermissionsUtil.requestPermission(this, new PermissionListener() {
            @Override
            public void permissionGranted(@NonNull String[] permission) {
                Toast.makeText(MainActivity.this, "权限申请成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void permissionDenied(@NonNull String[] permission) {
                Toast.makeText(MainActivity.this, "权限申请失败", Toast.LENGTH_SHORT).show();
            }
        }, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE});
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0,0,0,"添加");
        menu.add(1,1,1,"删除");
        menu.add(2,2,2,"修改");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 0:
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwind, null);
                final EditText name = inflate.findViewById(R.id.et_name);
                final EditText sex = inflate.findViewById(R.id.et_sex);
                final EditText age = inflate.findViewById(R.id.et_age);
                Button add = inflate.findViewById(R.id.btn_add);
                Button quxiao = inflate.findViewById(R.id.btn_quxiao);
                final PopupWindow pw = new PopupWindow(inflate, 1200, 600);
                pw.setBackgroundDrawable(new ColorDrawable());
                pw.setOutsideTouchable(true);
                pw.setFocusable(true);
                bgAlpha(0.5f);
                pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        bgAlpha(1f);
                    }
                });
                pw.showAtLocation(mRlv, Gravity.CENTER,100,100);
                add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name1 = name.getText().toString();
                        String sex1 = sex.getText().toString();
                        String age1 = age.getText().toString();
                        if (!TextUtils.isEmpty(name1) && !TextUtils.isEmpty(sex1) && !TextUtils.isEmpty(age1)){
                            MySql mySql = new MySql(MainActivity.this, "my.db", null, 1);
                            int age2 = Integer.parseInt(age1);
                            ArrayList<Bean> query = mySql.Query();
                            Bean bean = new Bean(query.size() + 1, name1, sex1, age2);
                            mySql.insert(bean);
                            beans.add(bean);
                            rlvAdapter.notifyDataSetChanged();
                            Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                            pw.dismiss();
                        }else {
                            Toast.makeText(MainActivity.this, "输入部门为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                quxiao.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                    }
                });
                break;
            case 1:
                MySql mySql = new MySql(MainActivity.this, "my.db", null, 1);
                mySql.delete(beans.get(mPosition));
                beans.remove(mPosition);
                rlvAdapter.notifyDataSetChanged();
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                View inflate1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwind, null);
                final EditText name1 = inflate1.findViewById(R.id.et_name);
                final EditText sex1 = inflate1.findViewById(R.id.et_sex);
                final EditText age1 = inflate1.findViewById(R.id.et_age);
                Button add1 = inflate1.findViewById(R.id.btn_add);
                Button quxiao1 = inflate1.findViewById(R.id.btn_quxiao);
                add1.setText("修改");
                Bean bean = beans.get(mPosition);
                String name2 = bean.getName();
                String sex2 = bean.getSex();
                int age2 = bean.getAge();
                name1.setText(name2);
                sex1.setText(sex2);
                age1.setText(age2+"");
                final PopupWindow pw1 = new PopupWindow(inflate1, 1200, 600);
                pw1.setBackgroundDrawable(new ColorDrawable());
                pw1.setOutsideTouchable(true);
                pw1.setFocusable(true);
                bgAlpha(0.5f);
                pw1.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        bgAlpha(1f);



                    }
                });
                pw1.showAtLocation(mRlv, Gravity.CENTER,100,100);
                add1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name2 = name1.getText().toString();
                        String sex2 = sex1.getText().toString();
                        String age2 = age1.getText().toString();
                        if (!TextUtils.isEmpty(name2) && !TextUtils.isEmpty(sex2) && !TextUtils.isEmpty(age2)){
                            MySql mySql = new MySql(MainActivity.this, "my.db", null, 1);
                            int age3 = Integer.parseInt(age2);
                            ArrayList<Bean> query = mySql.Query();
                            Bean bean = new Bean(mPosition, name2, sex2, age3);
                            mySql.UpData(beans.get(mPosition),bean);
                            beans.get(mPosition).setAge(age3);
                            beans.get(mPosition).setName(name2);
                            beans.get(mPosition).setSex(sex2);
                            rlvAdapter.notifyDataSetChanged();
                            Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            pw1.dismiss();
                        }else {
                            Toast.makeText(MainActivity.this, "输入部门为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                quxiao1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw1.dismiss();
                    }
                });
                break;
        }
        return super.onContextItemSelected(item);
    }
    public void bgAlpha(float alpha){
        WindowManager.LayoutParams ab = getWindow().getAttributes();
        ab.alpha = alpha;
        getWindow().setAttributes(ab);
    }
}
